package com.ysstech.reportworld.controller;


import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.enums.YssEnum;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.fileutil.FileWriterUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.DataSql;
import com.ysstech.reportworld.entity.DemandBugInfo;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.enums.ReportWorldFileNameEnum;
import com.ysstech.reportworld.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报表世界SQL信息表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-05-19
 */
@Controller
@Slf4j
@Api(tags = "报表世界SQL管理api")
@RequestMapping("/dataSql")
public class DataSqlController extends BaseController {
    @Autowired
    private DataSqlService dataSqlService;
    @Autowired
    private ModularService modularService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private DemandBugInfoService demandBugInfoService;

    /**
     * 跳转SQL管理页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:datasql"})
    @ApiOperation(value = "跳转SQL管理页面")
    @YssLog(module = "SQL管理", remark = "跳转SQL管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "reportworld/datasql";
    }


    @YssPermissions(value = {"reportworld:datasql"})
    @ApiOperation(value = "获取SQL管理页面初始值")
    @YssLog(module = "SQL管理", remark = "获取SQL管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 状态
        List<Dicvalue> listStatus = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_STATUS.getCode());
        map.put("listStatus", listStatus);
        map.put("listStatus2", listStatus);
        // SQL类型
        List<Dicvalue> type = dicValueService.getCacheDicValueList(ReportWorldEnum.DATASQL.getCode());
        map.put("type", type);
        map.put("type2", type);
        // 所属模块
        List<TreeNode> listModular = modularService.queryModularTree();
        map.put("listModular", listModular);
        // 所属客户
        List<TreeNode> listCustomer = customerService.queryCustomerTree();
        map.put("listCustomer", listCustomer);
        // 所属报表
        List<TreeNode> listReport = reportService.queryReportTree();
        map.put("listReport", listReport);
        // 版本
        List<Dicvalue> listVersion = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_VERSION.getCode());
        map.put("listVersion", listVersion);
        map.put("listVersion2", listVersion);
        // 数据库类型
        List<Dicvalue> listReportDataBaseType = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_DATABASE_TYPE.getCode());
        map.put("listReportDataBaseType", listReportDataBaseType);
        map.put("listReportDataBaseType2", listReportDataBaseType);
        // 当前版本的所有需求和BUG
        List<DemandBugInfo> demandBugInfoList = demandBugInfoService.selectByPrimaryByVersion();
        map.put("demandBugInfoList", demandBugInfoList);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * SQL列表查询
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:datasql"})
    @ApiOperation(value = "获取所有SQL信息")
    @YssLog(module = "SQL管理", remark = "获取所有SQL信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(DataSql dataSql, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取所有SQL信息成功",
                dataSqlService.queryAllDataSqls(dataSql, this.getPage(page, rows)));
    }

    /**
     * 新增SQL的方法
     *
     * @param dataSql
     * @return
     */
    @YssPermissions(value = {"reportworld:datasql:add"})
    @ApiOperation(value = "新增SQL信息")
    @YssLog(module = "SQL管理", remark = "新增SQL信息")
    @RequestMapping(value = "/addDataSql")
    @ResponseBody
    public RestResult addDataSql(DataSql dataSql) throws Exception {
        if (null == dataSql) {
            throw new BusinessException("获取新增报表世界SQL信息失败，请稍后重试！");
        }
        dataSql.setAddTime(LocalDateTime.now());
        dataSql.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        dataSqlService.addReportDataSql(dataSql);
        return ResultGenerator.getSuccessResult("新增SQL信息成功！");
    }

    /**
     * 修改SQL信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:datasql:edit"})
    @ApiOperation(value = "修改SQL信息")
    @YssLog(module = "SQL管理", remark = "修改SQL信息")
    @RequestMapping(value = "/editDataSql")
    @ResponseBody
    public RestResult editDataSql(DataSql dataSql) throws Exception {
        if (dataSql == null || StringUtils.isBlank(dataSql.getId())) {
            throw new BusinessException("获取SQL信息失败，请稍后重试！");
        }
        dataSql.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        dataSql.setEditTime(LocalDateTime.now());
        dataSqlService.updateReportDataSql(dataSql);
        return ResultGenerator.getSuccessResult("修改SQL信息成功！");
    }

    /**
     * 删除SQL信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:datasql:del"})
    @ApiOperation(value = "删除SQL信息")
    @YssLog(module = "SQL管理", remark = "删除SQL信息")
    @RequestMapping(value = "/delDataSql")
    @ResponseBody
    public RestResult delDataSql(String ids) throws Exception {
        dataSqlService.deleteReportDataSql(ids);
        return ResultGenerator.getSuccessResult("删除SQL信息成功！");
    }
}
